package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.badibadi.activity.RigionPictureActivity.RigionPictureFragment02;
import com.badibadi.infos.JuLeBan_Model;
import com.badibadi.infos.PhotoIdToInfoModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.id;
import com.badibadi.uniclubber.R.string;
//import com.lidroid.xutils.util.LogUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ClubSpaceModifyTheClubPolyActivity extends ClubSpacePolyActivity {

	private Results results, results2;
	private JuLeBan_Model ban_Model;
	private String photoid, cid, room_status;
	private EditText activity_club_space_poly_name;
	private LinearLayout create_new_poly_layout01, create_new_poly_layout02;
	private PhotoIdToInfoModel photoIdToInfoModel;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ClubSpaceModifyTheClubPolyActivity.this);
				Utils.showMessage(ClubSpaceModifyTheClubPolyActivity.this,
						getResources().getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(ClubSpaceModifyTheClubPolyActivity.this);
				setData();
				break;
			case 3:
				Utils.ExitPrgress(ClubSpaceModifyTheClubPolyActivity.this);
				Utils.showMessage(ClubSpaceModifyTheClubPolyActivity.this,
						getResources().getString(R.string.l_xa10));
				break;
			case 4:
				Utils.ExitPrgress(ClubSpaceModifyTheClubPolyActivity.this);
				Utils.showMessage(ClubSpaceModifyTheClubPolyActivity.this,
						getResources().getString(R.string.l_xb55));
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		init();
		try {
			photoid = getIntent().getStringExtra("photoid");
			cid = getIntent().getStringExtra("cid");
			photoIdToInfoModel=(PhotoIdToInfoModel) getIntent().getSerializableExtra("photoIdToInfoModel");
		} catch (Exception e) {
			   
		}
//		photoid = "214";
//		cid = "57";
		
		if(photoIdToInfoModel!=null)
		{//这里如果是活动相册不能编辑名字
//			LogUtils.d("photoIdToInfoModel.toString()"+photoIdToInfoModel.toString());
			if(!photoIdToInfoModel.getActivity_id().equals("0") && !(""+photoIdToInfoModel.getActivity_id()).equals("null"))
			{
//				LogUtils.d("photoIdToInfoModel.getActivity_id="+photoIdToInfoModel.getActivity_id());
				this.findViewById(R.id.ll_name).setVisibility(View.GONE);
			}
			
		}
		show_photo(photoid);
		this.findViewById(R.id.activity_club_space_poly_finish)
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
//						   ("开始修改");
						editClubPhoto();
					}
				});
		this.findViewById(R.id.activity_club_space_poly_back)
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						finish();
					}
				});
	}

	private void init() {
		((TextView) (this.findViewById(R.id.activity_club_space_poly_title)))
				.setText(R.string.ModifyTheClubPoly);
		activity_club_space_poly_name = (EditText) findViewById(R.id.activity_club_space_poly_name);
		create_new_poly_layout02 = (LinearLayout) findViewById(R.id.create_new_poly_layout02);
		create_new_poly_layout01 = (LinearLayout) findViewById(R.id.create_new_poly_layout01);
	}

	private void setData() {
		try {
			activity_club_space_poly_name.setText(ban_Model.getName());
			setPermission(Integer.valueOf(ban_Model.getRoom_status()));
		} catch (Exception e) {
			   
		}
	}

	@Override
	public void setPermission(int permission) {
		if (permission == 1) {
			this.findViewById(R.id.cnewpoly_quanxian_01).setVisibility(
					View.VISIBLE);
			this.findViewById(R.id.cnewpoly_quanxian_02).setVisibility(
					View.GONE);
			room_status = "1";
		} else if (permission == 0) {
			this.findViewById(R.id.cnewpoly_quanxian_01).setVisibility(
					View.GONE);
			this.findViewById(R.id.cnewpoly_quanxian_02).setVisibility(
					View.VISIBLE);
			room_status = "0";
		}
	}

	private void show_photo(final String photoid) {
		Utils.showPrgress(ClubSpaceModifyTheClubPolyActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("photoId", photoid);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "photo/show_photo");
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						try {
//							   ("获取到的聚乐版内容是:" + content);
							ban_Model = new JuLeBan_Model();
							ban_Model = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), JuLeBan_Model.class);
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}

			}
		});
	}

	private void editClubPhoto() {
		Utils.showPrgress(ClubSpaceModifyTheClubPolyActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				try {
					map.put("photo", fengzhuang_julebu_juleban());
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					   
				}
//				   ("提交修改俱乐部的聚乐版的数据是:" + map.toString());
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "photo/editClubPhoto");
				if (content != null) {
					results2 = Utils.checkResult_NNN(getApplicationContext(),
							content);
//					   ("获取到修改聚乐版内容是:" + content + " "
//							+ results2.getRetmsg());
					if (results2 != null
							&& !results2.getRetmsg().equals("null")
							&& results2.isRet()) {
						try {
							handler.sendEmptyMessage(4);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}

			}
		});
	}

	private JSONObject fengzhuang_julebu_juleban() throws JSONException {
		JSONObject object = new JSONObject();
		String na = activity_club_space_poly_name.getText().toString().trim();
		if (na != null) {
			object.put("cid", cid);
			object.put("photoId", photoid);
			object.put("uid",
					Utils.getUid(ClubSpaceModifyTheClubPolyActivity.this));
			object.put("room_status", room_status);
			//如果是活动相册  
			if(!photoIdToInfoModel.getActivity_id().equals("0") && !(""+photoIdToInfoModel.getActivity_id()).equals("null"))
			{
//				LogUtils.d("photoIdToInfoModel.getActivity_id="+photoIdToInfoModel.getActivity_id());
				this.findViewById(R.id.ll_name).setVisibility(View.GONE);
				object.put("name", photoIdToInfoModel.getPhotoName());
			}else
			{
				object.put("name", na);
			}
		
			object.put("is_mainboard", "0");
		}
		return object;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.create_new_poly_layout01:
			setPermission(1);
			break;
		case R.id.create_new_poly_layout02:
			setPermission(0);
			break;
		default:
			break;
		}
	}

}
